﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bookkeeping.Core.Entities;
using Bookkeeping.Mvc.Models.Budget;
using Bookkeeping.Mvc.Models.BudgetPost;
using Bookkeeping.Persistence.Repositories;

namespace Bookkeeping.Mvc.Controllers
{
    [HandleError]
    public class BudgetPostController : Controller
    {
        public ActionResult Add(int year, int month)
        {
            var dateTime = new DateTime(year, month, 1);
            var viewModel = new AddViewModel(year, month);

            return View(viewModel);
        }
        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(int year, int month, BindablePost post)
        {
            var date = new DateTime(year, month, 1);

            using (var repository = new PostRepository())
            {
                Post postToInsert = post.ToPost();
                postToInsert.DateTime = date;

                repository.Insert(postToInsert);
            }

            string budgetUrl = string.Format("~/{0}/{1}", year, month);
            return new RedirectResult(budgetUrl);
        }

        public ActionResult Edit(int id)
        {
            using (var repository = new PostRepository())
            {
                Post postToEdit = repository.Get(id);

                BindablePost flattenedPost = BindablePost.FromPost(postToEdit);
                return View(flattenedPost);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, BindablePost post)
        {
            throw new NotImplementedException();
            //using (var repository = new PostRepository())
            //{
            //    Post editedPost = post.ToPost();
            //    repository.AttachChanged(editedPost);

            //    return RedirectToAction("List", new { year = post. });
            //}
        }
    }
}
